#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BLOCKSIZE       (1 * 1024 * 1024)


int main(int argc, char *argv[])
{
    char *buf;

    if (argc != 2) {
        printf("sysalloc [n]        --  do malloc [n] MB\n");
        return 0;
    }

    int n = atoi(argv[1]);
    printf("target: %6d MB.\n", n);

    for (int i = 0; i < n; i++) {
        buf = malloc(BLOCKSIZE);
        if (buf == NULL) {
            printf("\n");
            printf("error: malloc failed!\n");
            return -1;
        }
        memset(buf, 0, BLOCKSIZE);
        printf("\rmalloc: %6d MB.", i + 1);
    }
    printf("\n");
    printf("done!\n");
    getchar();
    
    return 0;
}
